package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 分类分页查询
     *
     * @param categoryPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(CategoryPageQueryDTO categoryPageQueryDTO) {
        // 1. 开启分页功能
        PageHelper.startPage(categoryPageQueryDTO.getPage(), categoryPageQueryDTO.getPageSize());

        // 2. 执行分页查询
        Page<Category> page = categoryMapper.pageQuery(categoryPageQueryDTO);

        // 3. 封装分页结果
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 新增分类
     *
     * @param categoryDTO
     */
    @Override
    public void saveCategory(CategoryDTO categoryDTO) {
        // 1. 创建 Category 实体对象
        Category category = new Category();

        // 2. 对象属性拷贝
        BeanUtils.copyProperties(categoryDTO, category);

        // 3. 设置状态为0，默认禁用
        category.setStatus(0);

        // 4. 插入数据
        categoryMapper.insert(category);
    }

    /**
     * 根据id删除分类
     *
     * @param id
     */
    @Override
    public void deleteCategory(Long id) {
        // 1. 查询当前分类是否关联了菜品
        int dishCount = dishMapper.countByCategoryId(id);

        if (dishCount > 0) {
            // 1.1. 当前分类关联了菜品，不能删除
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }

        // 2. 查询当前分类是否关联了套餐
        int setmealCount = setmealMapper.countByCategoryId(id);
        if (setmealCount > 0) {
            // 2.1. 当前分类关联了套餐，不能删除
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }

        // 3. 删除分类
        categoryMapper.deleteById(id);
    }

    /**
     * 启用、禁用分类
     *
     * @param status
     * @param id
     */
    @Override
    public void startOrStopCategory(Integer status, Long id) {
        // 1. 构建用于更新状态的分类实体对象
        Category category = Category.builder()
                .id(id)
                .status(status)
                .build();

        // 2. 更新分类状态
        categoryMapper.update(category);
    }

    /**
     * 修改分类
     *
     * @param categoryDTO
     */
    @Override
    public void updateCategory(CategoryDTO categoryDTO) {
        // 1. 构建用于更新的分类实体对象
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);

        // 2. 更新分类
        categoryMapper.update(category);
    }

    /**
     * 根据id查询分类
     *
     * @param id
     * @return
     */
    @Override
    public Category getById(Long id) {
        return categoryMapper.selectById(id);
    }

    /**
     * 根据类型查询分类
     *
     * @param type
     * @return
     */
    @Override
    public List<Category> listCategory(Integer type) {
        return categoryMapper.listCategory(type);
    }
}
